/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2025-06-07 11:44:52
 * @LastEditors: sueRimn
 * @LastEditTime: 2025-08-31 18:21:02
 */
#include "stdio.h"

#include "power.h"


#include "step.h"
#include "led.h"
#include "dyp.h"
#include "public.h"
#include "key.h"

#include "task.h"



void delay_task_run(void);




void loop_task_init(void)
{

}

void loop_task_run(void)
{

    // messageOperation();

    delay_task_run();
    key_run();

    int16_t iDistance1 = 0,iDistance2 = 0,iDistance3 = 0;

    if ((pstDev->uiSysMsTime - pstDev->uiIcmRunMsTime) >= ICM_RUN_TASK_FREQ && pstDev->stDevPowerSupply.bPower24VPort1)
    {

        // canReportIcmAcc();
        // canReportIcmGyro();

        pstDev->uiIcmRunMsTime = pstDev->uiSysMsTime;
    }

    if ((pstDev->uiSysMsTime - pstDev->uiDypRunMsTime + 1) >= DYP_RUN_TASK_FREQ && pstDev->stDevPowerSupply.bPowerDev5VPort) //+1是为了防止任务挤在一起执行导致部分can数据无法发送
    {
        dypMonitRunTask();
        fallMonitRunTask();
        if (pstDev->stDevPowerSupply.bPower24VPort1)
            canReportDistance();
       
        if(iDistance1 != pstDev->stDevDyp1.iDistance ||
            iDistance2 != pstDev->stDevDyp2.iDistance ||
            iDistance3 != pstDev->stDevDyp3.iDistance)
        
       printf("distance 1 = %d mm  distance 2 = %d mm  distance 3 = %d mm\r\n", pstDev->stDevDyp1.iDistance, pstDev->stDevDyp2.iDistance, pstDev->stDevDyp3.iDistance);

        iDistance1 = pstDev->stDevDyp1.iDistance;
        iDistance2 = pstDev->stDevDyp2.iDistance;
        iDistance3 = pstDev->stDevDyp3.iDistance;

        pstDev->uiDypRunMsTime = pstDev->uiSysMsTime;
    }

    if ((pstDev->uiSysMsTime - pstDev->uiBatChargMsTime + 3) >= BAT_RUN_TASK_FREQ)
    {
        powerRunTask();
        // INA_Read(0);
        // INA_Read(1);

        if (pstDev->stDevPowerSupply.bPower24VPort1)
            canReportPower();


        // printf("BAT V = %d mv A = %d ma\r\n", pstDev->stDevBat.sVolVal, pstDev->stDevBat.sEleVal);
        // printf("CHARG V = %d mv A = %d ma\r\n", pstDev->stDevCharg.sVolVal, pstDev->stDevCharg.sEleVal);
        pstDev->uiBatChargMsTime = pstDev->uiSysMsTime;
    }

    if ((pstDev->uiSysMsTime - pstDev->uiLedMsTime) >= LED_RUN_TASK_FREQ)
    {
        ledRunTask();
        //		printf("run\r\n");
        pstDev->uiLedMsTime = pstDev->uiSysMsTime;
        //			printf(" read = %d\r\n",HAL_GPIO_ReadPin(FAST_STOP_GPIO_Port,FAST_STOP_Pin));
    }

}



task_delay_t pstHeadTaskNode;
task_delay_t pstTailTaskNode;

task_delay_t delay_task_regist(uint32_t uiTaskDelayTime, void (*task_callback)(void *param), void *param)
{
    task_delay_t pstTaskNode = (task_delay_t)malloc(sizeof(task_delay));
    // printf("pstKeyNode addr = %x\r\n", pstKeyNode);
    pstTaskNode->uiTaskDelayTime = uiTaskDelayTime;
    pstTaskNode->task_callback = task_callback;
    pstTaskNode->param = param;
    pstTaskNode->pstNextTask = NULL;

    return pstTaskNode;
}

void delay_task_add_run_list(task_delay_t pstAddTaskNode)
{
    pstTailTaskNode->pstNextTask = pstAddTaskNode;
    pstTailTaskNode = pstAddTaskNode;
    pstAddTaskNode->uiTaskStartTime = pstDev->uiSysMsTime;
}

void delay_task_del_run_list(task_delay_t pstDelTaskNode)
{
    task_delay_t pstTaskNode = pstHeadTaskNode;

    while (pstTaskNode->pstNextTask != pstDelTaskNode)
    {
        pstTaskNode = pstTaskNode->pstNextTask;
    }

    pstTaskNode->pstNextTask = pstDelTaskNode->pstNextTask;

    if (pstTailTaskNode == pstDelTaskNode)
    {
        pstTailTaskNode = pstTaskNode;
    }

    //延时任务不会太多 不做释放动作了
}

void delay_task_run()
{
    task_delay_t pstTaskNode = pstHeadTaskNode;

    while (pstTaskNode->pstNextTask != NULL)
    {
        pstTaskNode = pstTaskNode->pstNextTask;

        if (pstDev->uiSysMsTime - pstTaskNode->uiTaskStartTime >= pstTaskNode->uiTaskDelayTime)
        {
            pstTaskNode->task_callback(pstTaskNode->param);
            delay_task_del_run_list(pstTaskNode);
        }

    }
}

void delay_task_init()
{
    pstHeadTaskNode = (task_delay_t)malloc(sizeof(task_delay));
    pstTailTaskNode = pstHeadTaskNode;
}

void delay_task_display(void *param)
{
    printf("run task systemTime = %d\r\n", pstDev->uiSysMsTime);
}

void delay_task_test()
{
    task_delay_t pstTaskNode = delay_task_regist(1000, delay_task_display, NULL);
    task_delay_t pstTaskNode2 = delay_task_regist(1000, delay_task_display, NULL);
    delay_task_add_run_list(pstTaskNode);
    printf("add task1 systemTime = %d\r\n", pstDev->uiSysMsTime);
    HAL_Delay(1200);
    delay_task_add_run_list(pstTaskNode2);
    printf("add task2 systemTime = %d\r\n", pstDev->uiSysMsTime);
}
